﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WoWConnecter
{
    public partial class RealmClient
    {

        [PacketMethod(RealmServerOpCode.SMSG_AUTH_CHALLENGE)]
        private void HandleAuthChallenge(RealmPacketIn packet)
        {
            uint random = packet.ReadUInt32(); // 1
            ServerSeed = packet.ReadUInt32();
            ClientSeed = (uint)new Random().Next();

            RealmPacketOut packetOut = new RealmPacketOut(RealmServerOpCode.CMSG_AUTH_SESSION);
            packetOut.WriteUInt(11403);
            packetOut.WriteUInt(0); //Unknown
            packetOut.Write(Username);
            packetOut.WriteUInt(0); //Unknown
            packetOut.Write(ClientSeed);
            packetOut.WriteULong(0); //Unknown
            byte[] digest = SecurePassword.Hash(Username, new byte[4], ClientSeed, ServerSeed, SecurePassword.SessionKey).GetBytes();
            packetOut.Write(digest);

            RealmPacketOut compressed = new RealmPacketOut();
            compressed.Write(0);
            //compressed.CompressInto(packetOut);
            Send(packetOut);

            PacketEncryption = new PacketCrypt(SecurePassword.SessionKey.GetBytes(40));
        }

        [PacketMethod(RealmServerOpCode.SMSG_AUTH_RESPONSE)]
        private void HandleAuthResponse(RealmPacketIn packet)
        {
            LoginErrorCode result = (LoginErrorCode)packet.ReadByte();

            if (result == LoginErrorCode.AUTH_OK)
            {
                int billingTime = packet.ReadInt32();
                byte billingFlags = packet.ReadByte();
                byte clientId = packet.ReadByte();

                Send(new RealmPacketOut(RealmServerOpCode.CMSG_CHAR_ENUM));
            }

        }
    }
}
